#! /usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright © 2016 lizongzhe 
#
# Distributed under terms of the MIT license.
#from proxy_manager.resource.base import BaseProxyResource
import re
import logging
import urllib
import requests
from bs4 import BeautifulSoup
from proxy_manager.core import ProxyHost
from proxy_manager.resource.base import BaseProxyResource
import random

logger = logging.getLogger(__name__)

COUNTRIES = (
        ("ALL", ""),
        ("CN", urllib.quote_plus("中国")),
        ("DK", urllib.quote_plus("丹麦")),
        ("UA", urllib.quote_plus("乌克兰")),
        ("UY", urllib.quote_plus("乌拉圭")),
        ("AM", urllib.quote_plus("亚美尼亚")),
        ("IL", urllib.quote_plus("以色列")),
        ("IQ", urllib.quote_plus("伊拉克")),
        ("IR", urllib.quote_plus("伊朗")),
        ("RU", urllib.quote_plus("俄罗斯")),
        ("BG", urllib.quote_plus("保加利亚")),
        ("CA", urllib.quote_plus("加拿大")),
        ("HU", urllib.quote_plus("匈牙利")),
        ("ZA", urllib.quote_plus("南非")),
        ("RW", urllib.quote_plus("卢旺达")),
        ("IN", urllib.quote_plus("印度")),
        ("ID", urllib.quote_plus("印度尼西亚")),
        ("EC", urllib.quote_plus("厄瓜多尔")),
        ("TW", urllib.quote_plus("台湾")),
        ("KG", urllib.quote_plus("吉尔吉斯斯坦")),
        ("KZ", urllib.quote_plus("哈萨克斯坦")),
        ("CO", urllib.quote_plus("哥伦比亚")),
        ("TR", urllib.quote_plus("土耳其")),
        ("EG", urllib.quote_plus("埃及")),
        ("RS", urllib.quote_plus("塞尔维亚")),
        ("SL", urllib.quote_plus("塞拉利昂")),
        ("MX", urllib.quote_plus("墨西哥")),
        ("DO", urllib.quote_plus("多米尼加共和国")),
        ("AT", urllib.quote_plus("奥地利")),
        ("VE", urllib.quote_plus("委内瑞拉")),
        ("BD", urllib.quote_plus("孟加拉国")),
        ("NG", urllib.quote_plus("尼日利亚")),
        ("NP", urllib.quote_plus("尼泊尔")),
        ("PK", urllib.quote_plus("巴基斯坦")),
        ("PY", urllib.quote_plus("巴拉圭")),
        ("PA", urllib.quote_plus("巴拿马")),
        ("BR", urllib.quote_plus("巴西")),
        ("GR", urllib.quote_plus("希腊")),
        ("DE", urllib.quote_plus("德国")),
        ("IT", urllib.quote_plus("意大利")),
        ("LV", urllib.quote_plus("拉脱维亚")),
        ("CZ", urllib.quote_plus("捷克")),
        ("MD", urllib.quote_plus("摩尔多瓦")),
        ("SK", urllib.quote_plus("斯洛伐克")),
        ("SG", urllib.quote_plus("新加坡")),
        ("JP", urllib.quote_plus("日本")),
        ("CL", urllib.quote_plus("智利")),
        ("KH", urllib.quote_plus("柬埔寨")),
        ("GE", urllib.quote_plus("格鲁吉亚")),
        ("EU", urllib.quote_plus("欧洲")),
        ("BE", urllib.quote_plus("比利时")),
        ("FR", urllib.quote_plus("法国")),
        ("PL", urllib.quote_plus("波兰")),
        ("TH", urllib.quote_plus("泰国")),
        ("ZW", urllib.quote_plus("津巴布韦")),
        ("AU", urllib.quote_plus("澳大利亚")),
        ("MO", urllib.quote_plus("澳门")),
        ("TT", urllib.quote_plus("特里尼达和多巴哥")),
        ("BO", urllib.quote_plus("玻利维亚")),
        ("SE", urllib.quote_plus("瑞典")),
        ("CH", urllib.quote_plus("瑞士")),
        ("PE", urllib.quote_plus("秘鲁")),
        ("RO", urllib.quote_plus("罗马尼亚")),
        ("US", urllib.quote_plus("美国")),
        ("KE", urllib.quote_plus("肯尼亚")),
        ("GB", urllib.quote_plus("英国")),
        ("NL", urllib.quote_plus("荷兰")),
        ("PH", urllib.quote_plus("菲律宾")),
        ("ES", urllib.quote_plus("西班牙")),
        ("ZM", urllib.quote_plus("赞比亚")),
        ("VN", urllib.quote_plus("越南")),
        ("AZ", urllib.quote_plus("阿塞拜疆")),
        ("AL", urllib.quote_plus("阿尔巴尼亚")),
        ("AR", urllib.quote_plus("阿根廷")),
        ("AE", urllib.quote_plus("阿联酋")),
        ("KR", urllib.quote_plus("韩国")),
        ("HK", urllib.quote_plus("香港")),
        ("MV", urllib.quote_plus("马尔代夫")),
        ("MY", urllib.quote_plus("马来西亚")),
        ("LB", urllib.quote_plus("黎巴嫩")),
)


SCHEMES = (
    ('http', '1'),
    ('https', '2'),
    ('socket4', '3'),
    ('socket5', '4')
)

AVAILABILITY = [(value, ">={}%".format(value)) for value in range(0, 100, 10)]

class ProxyResource(BaseProxyResource):
    api = 'http://proxy.mimvp.com/api/fetch.php?orderid={}&num={}&country={}&http_type={}&ping_time=1&transfer_time=5&result_fields=1,2&result_format=json'
    def __init__(self, orderid, country='', scheme='http', limit=2000):
        self.orderid = orderid

        self.country = country.upper()
        country_code = dict(COUNTRIES)[country]

        self.scheme = scheme.lower()
        scheme_code = dict(SCHEMES)[self.scheme]

        self.url = self.api.format(orderid, limit, country_code, scheme_code)

    def download(self):
        resp = requests.get(self.url)   
        data = resp.json()
        proxy_list = data['result']
        random.shuffle(proxy_list)
        result = []
        for proxy in proxy_list:
            ip, port = proxy['ip:port'].split(':')
            scheme = proxy['http_type']
            proxy_host = ProxyHost(ip=ip, port=port, scheme=scheme.lower())
            result.append(proxy_host)
        return result

    def get_proxies(self):
        try:
            data = self.download()
            return data
        except Exception as e:
            logger.error(e, exc_info=True)
            raise e



